package com.mj.hotel.mvp.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.mj.hotel.R;
import com.mj.hotel.mvp.ui.event.ChangeFragmentMsg;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

public class RoomListFragment extends BaseFragment {

    @BindView(R.id.roomVp)
    public ViewPager roomVp;
    private RoomPagerAdapter roomPagerAdapter;

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_roomlist, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ArrayList<RecyclerView> roomPages = new ArrayList<RecyclerView>();
        for(int i = 0; i < 3; i++) {
            RecyclerView one = new RecyclerView(mContext);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
            one.addItemDecoration(new ActionItemDecoration(3, 10, false));
            one.setHasFixedSize(true);
            one.setLayoutManager(gridLayoutManager);
            one.setAdapter(new RoomRcvAdapter());
            roomPages.add(one);
        }
        roomPagerAdapter = new RoomPagerAdapter(roomPages);
        roomVp.setAdapter(roomPagerAdapter);
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    class RoomItemViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        public RoomItemViewHolder(View view) {
            super(view);
            rootView = view;
        }
    }

    class RoomRcvAdapter extends RecyclerView.Adapter<RoomItemViewHolder>{

        @NonNull
        @Override
        public RoomListFragment.RoomItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.roomlist_item, parent, false);
            return new RoomItemViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RoomItemViewHolder holder, final int position) {
            holder.rootView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(ChangeFragmentMsg.getInstance(RoomDetailFragment.class.getSimpleName()));
                }
            });
        }

        @Override
        public int getItemCount() {
            return 6;
        }
    }

    class ActionItemDecoration extends RecyclerView.ItemDecoration{
        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public ActionItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }

    public class RoomPagerAdapter extends PagerAdapter {
        private ArrayList<RecyclerView> mList;
        public RoomPagerAdapter(ArrayList<RecyclerView> mList) {
            this.mList = mList;
        }
        @Override
        public int getCount() {
            Log.i("lzl","getcount " + mList.size());
            return mList.isEmpty() ? 0 : mList.size();
        }
        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view==o;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mList.get(position));
            return mList.get(position);
        }
    }
}
